<?php
namespace App\Http\Middleware;

/**
 * This file is part of Entrust,
 * a role & permission management solution for Laravel.
 *
 * @license MIT
 * @package Zizaco\Entrust
 */

use App\Models\Admin\Role;
use Closure;
use Auth;
use Request;

class AdminRole
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  Closure $next
     * @param  $roles
     * @return mixed
     */
    public function handle($request, Closure $next, $roles)
    {
        if(!in_array(Request::getPathInfo(),config('admin.admin_uneed_check'))){
            if(!Auth::guard('admin')->check()){
                return redirect('/admin/login');
            }

            if (!Auth::guard('admin')->user()->canRole(explode('|', $roles))) {
//                $roles = Role::where('name', explode('|', $roles))->get();
//                $err = "";
//                foreach($roles as $role){
//                    if(str_word_count($err) > 1){
//                        $err .= '，';
//                    }
//                    $err .= $role->display_name;
//                }
                return redirect()->back()->with('error','您没有对应角色，不允许访问');
            }
        }

        return $next($request);
    }
}
